<?php
namespace Macaca\Module;

use Macaca;
use Monkey;

class SiteSet {

    public
        /**
         * @var Monkey\App
         */
        $app, 
        $allSet,

        $site_enable = true, //是否关闭网站 	否 是
        $site_map_enable = false, //是否启用站点地图
        $site_template_style = 'default', //站点模板
        $site_name = '我的网站', //    站点名称
        $site_title = 'MonkeyPHP Demo网站', //站点标题
        $site_key = 'Macaca, MonkeyPHP, Demo', //站点关键字
        $site_description = 'Macaca 是轻量级企业网站管理系统', //站点描述
        $site_logo = '', //站点标志文件
        $site_icp = '', //ICP备案证书号

        $company_address = '', //公司地址
        $company_service_call = '', //客服电话
        $company_fax = '', //传真
        $company_service_qq = '', //客服QQ号码，多个客服的QQ号码请以半角逗号（,）分隔。
        $company_service_email = '', //邮件地址

        $products_list_limit = 10, //商品列表数量
        $article_list_limit = 10, //文章列表数量
        $guestbook_list_limit = 10, //留言列表数量
        $thumbnail_width = 135, //商品列表数量
        $thumbnail_height = 135 //商品列表数量
    ;

    /**
     * @param Monkey\App $app
     */
    public function __construct($app) {
        $this->app = $app;
        $this->allSet = $app->config()->get('siteset', array());

        foreach ($this->allSet as $k => $v) {
            if (property_exists($this, $k)) {
                $this->$k = $v;
            }
        }
    }

    public function saveSet($data = array()) {
        $data = empty($data) ? $this->propertyToData() : $this->formatData($data);

        $config = $this->app->config();
        $config->set('siteset', $data);
        $config->update();
    }

    private function formatData($data) {
        foreach ($data as $k => $v) {

            if (is_numeric($v)) {
                $data[$k] = $v + 0;

            }
            elseif (strtolower($v) == 'true') {
                $data[$k] = true;

            }
            elseif (strtolower($v) == 'false') {
                $data[$k] = false;

            }
            else {
                $data[$k] = trim(strip_tags($v));
            }

        }

        return $data;
    }

    private function propertyToData() {
        foreach ($this->allSet as $k => $v) {
            if (property_exists($this, $k)) {
                $this->allSet[$k] = $this->$k;
            }
        }

        return $this->allSet;
    }

} 